﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KVB.Common;
using Customer_Profile.eCard.DBDao;
using Customer_Profile.Admin;

namespace Customer_Profile.eCard.Settle
{
    public partial class SettleTransfer : System.Web.UI.Page
    {
        string strUserName = "";
        string strUserID = "";
        string strEntityID = "";
        string strEntity = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Buffer = true;
            Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
            Response.Expires = 0;
            Response.CacheControl = "no-cache";

            Comm.CheckSession(this.Page, "/login.aspx", "usermodel");
            if (Session["usermodel"] != null)
            {
                strUserID = ((UserInfoEntity)Session["usermodel"]).UserLoginId;
                strUserName = ((UserInfoEntity)Session["usermodel"]).UserName;
                strEntityID = ((UserInfoEntity)Session["usermodel"]).UserEntityID;
                strEntity = ((UserInfoEntity)Session["usermodel"]).UserOffice;
            }
            else
            {
                Log.WriteDefaultLog("main_01 Page_Load", "Session is null");
                return;
            }
            string strURL = HttpContext.Current.Request.Url.AbsolutePath;
            bool bok = Comm.ifhasright(strUserID, strURL);
            if (!bok)
            {
                Response.Write("你无权访问此页面");
                Response.End();
            }

            if (!Page.IsPostBack)
            {
                //Comm.BindDll(ref dropTerm, "TermType", true, "");
                DataSet dsProd = ProductDao.getAllProduct("ALL");
                dropProd.DataSource = dsProd;
                dropProd.DataTextField = "procduct_name";
                dropProd.DataValueField = "procduct_code";
                dropProd.DataBind();
                ListItem liAll = new ListItem();
                liAll.Text = "All";
                liAll.Value = "All";
                dropProd.Items.Insert(0, liAll);

                //Comm.BindDll(ref dropEntity, "Entity", true, "");
                DataSet dsEntity = EntityDao.getEntityList("N");
                dropEntity.DataSource = dsEntity;
                dropEntity.DataTextField = "entity_name";
                dropEntity.DataValueField = "entity_id";
                dropEntity.DataBind();
                //ListItem liAllEntity = new ListItem();
                //liAllEntity.Text = "All";
                //liAllEntity.Value = "All";
                //dropEntity.Items.Insert(0, liAllEntity);
                dropEntity.Items.FindByValue(strEntityID).Selected = true;
                dropEntity.Enabled = false;

                txtcreatedatebegin.Text = DateTime.Now.ToString("yyyy-MM-dd");
                txtcreatedateend.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }
            bindGrid();
        }

        private void bindGrid()
        {
            try
            {
                string strWhere = " where ss.settle_status='SP' ";
                if (txtCardNo.Text.Trim() != "")
                    strWhere += " and d.card_no ='" + txtCardNo.Text.Trim() + "'";
                if (txtCpartyName.Text.Trim() != "")
                    strWhere += " and c.cparty_name like '%" + txtCpartyName.Text.Trim() + "%'";
                if (dropProd.SelectedIndex > 0)
                {
                    Log.WriteDefaultLog("", dropProd.SelectedItem.Value);
                    strWhere += " and d.prod_code = '" + dropProd.SelectedItem.Value + "'";
                }
                if (txtcreatedatebegin.Text != "" && txtcreatedateend.Text != "")
                    strWhere += " and convert(date,d.due_date) between '" + txtcreatedatebegin.Text.Trim() + "' and '" + txtcreatedateend.Text.Trim() + "'";

                DataSet ds = DealDao.getCardDealsneedSettle(strWhere);
                if (ds != null && ds.Tables.Count > 0)
                {
                    GvData1.DataSource = ds.Tables[0];
                    GvData1.DataBind();
                }
                else
                {
                    GvData1.DataBind();
                }
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("bindGrid", ex.ToString());
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        protected void btnExtend_Click(object sender, EventArgs e)
        {
            //window.location = "ExtendAdd.aspx?card_no='"+ strcardno +"'";
            string settle_id = hidsettleid.Value.Substring(0, hidsettleid.Value.Length - 1);
            Response.Redirect("TransferAdd.aspx?settle_id=" + settle_id);
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            string status="";
            string message="";
            string settle_id = hidsettleid.Value.Substring(0, hidsettleid.Value.Length - 1);
            DealDao.BackReView(settle_id, out status, out message);
            if (status.Equals("Y"))
            {
                bindGrid();
                GvData1.Selection.UnselectAll();
                hidsettleid.Value = "";
                KVB.Common.JavaScript.ViewAlertMsg("退回成功！可以在结算审核中重新审核了!");
            }
            else
            {
                Log.WriteDefaultLog("退回失败:",message);
                hidsettleid.Value = "";
                KVB.Common.JavaScript.ViewAlertMsg("退回失败！请联系管理员");
            }
        }
    }
}